package edu.gxmzu.task.mapper;

import edu.gxmzu.task.entity.Attachments;
import edu.gxmzu.task.models.TaskBoardModel;
import edu.gxmzu.task.models.WorkModel;
import edu.gxmzu.task.params.SearchTaskParam;
import edu.gxmzu.task.params.UpdateAdminWorkParam;
import edu.gxmzu.task.params.UpdateUserWorkParam;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import java.util.List;


@Mapper
public interface WordMapper {


    @Select("select w.id,w.user_id,u.name,w.appointor_id,u1.`name` as appointor_name, w.work_status_id,ws.`name` as work_status_name,w.priority_id,w.attachments_id,a.`name` as attachments_name,w.content,w.`require`,w.create_time,w.update_time " +
            "from works as w " +
            "INNER JOIN work_status as ws ON w.work_status_id = ws.id " +
            "INNER JOIN attachments as a ON a.id = w.attachments_id " +
            "INNER JOIN user_info as u on w.user_id = u.user_id " +
            "INNER JOIN user_info as u1 on u1.user_id = w.appointor_id " +
            "where w.user_id = #{userId}")
    List<WorkModel> getWorkByUserId(Long userId);


    @Update("update attachments set `name` = #{name}, `hash`= #{hash},`type`=#{type},`size`=#{size},`path`=#{path} where id = #{id}")
    int updateUserAttachment(Attachments attachments);


    @Update("update works set `content` = #{content}, `require` = #{require}, `update_time`=#{updateTime},`end_time`=#{endTime},`attachments_id` = #{attachmentsId},`work_status_id` = #{workStatusId} where id = #{id}")
    int updateUserWork(UpdateUserWorkParam updateUserWorkParam);


    @Update("update works set `user_id`=#{userId},`content` = #{content}, `require` = #{require}, `update_time`=#{updateTime},`end_time`=#{endTime}, `work_status_id` = #{workStatusId},`attachments_id` = #{attachmentsId} where id = #{id}")
    int updateAdminWork(UpdateAdminWorkParam updateAdminWorkParam);


    @Select("select w.all_task,w.to_do_task,w.completed_task,w.expired_task,m.unread_message " +
            "from (SELECT COUNT(*) AS all_task,COUNT(IF(w.work_status_id = 0,1,NULL)) as to_do_task,COUNT(IF(w.work_status_id = 1,1,NULL)) as completed_task,COUNT(IF(w.work_status_id = 2,1,NULL)) as expired_task,w.user_id " +
            "FROM works as w " +
            "where user_id = #{userId} ) as w, " +
            "(select COUNT(*) as unread_message from message where message.status_id = 0 and message.user_id = #{userId}) as m ")
    TaskBoardModel getAllWorks(String userId);

    @Select({"<script>",
            "SELECT * FROM works",
            "WHERE user_id = #{userId} and deletion_state = 0",
            "<when test='title!=null'>",
            "AND title like '%${title}%'",
            "</when>",
            "<when test='content!=null'>",
            "AND content like '%${content}%'",
            "</when>",
            "<when test='createTime!=null'>",
            "AND create_time <![CDATA[ >= ]]> '${createTime}'",
            "</when>",
            "<when test='endTime!=null'>",
            "AND '${endTime}' <![CDATA[ <= ]]> end_time  ",
            "</when>",
            "<when test='endTime!=null'>",
            "AND '${endTime}' <![CDATA[ >= ]]> create_time  ",
            "</when>",
            "</script>"})
    @Results(id = "SearchTask", value = {
            @Result(property = "attachmentId", column = "attachments_id", javaType = String.class, jdbcType = JdbcType.BIGINT, id = true),
            @Result(property = "userId", column = "user_id", javaType = String.class, jdbcType = JdbcType.BIGINT, id = true),
            @Result(property = "workStatusId", column = "work_status_id", javaType = int.class, jdbcType = JdbcType.BIGINT, id = true),
            @Result(property = "appointorId", column = "appointor_id", javaType = String.class, jdbcType = JdbcType.BIGINT, id = true),
            @Result(property = "attachmentName",column = "attachments_id", one = @One(select = "edu.gxmzu.task.mapper.WordMapper.getAttachmentsName", fetchType = FetchType.LAZY)),
            @Result(property = "userName",column = "user_id", one = @One(select = "edu.gxmzu.task.mapper.WordMapper.getUserName", fetchType = FetchType.LAZY)),
            @Result(property = "workStatusName",column = "work_status_id", one = @One(select = "edu.gxmzu.task.mapper.WordMapper.getStatusName", fetchType = FetchType.LAZY)),
            @Result(property = "appointorName",column = "appointor_id", one = @One(select = "edu.gxmzu.task.mapper.WordMapper.getAppointorName", fetchType = FetchType.LAZY)),
    })
    List<WorkModel> SearchTask(SearchTaskParam searchTaskParam);

    @Select("SELECT name FROM attachments WHERE id = #{attachmentsId}")
    @Results(id = "AttachmentsMap", value = {
            @Result(property = "attachmentName", column = "name"),})
    String getAttachmentsName(@Param("attachments_id")String attachmentsId);

    @Select("SELECT name FROM user_info WHERE user_id = #{userId}")
    @Results(id = "getUserNameMap", value = {
            @Result(property = "userName", column = "name"),})
    String getUserName(@Param("user_id")String userId);

    @Select("SELECT name FROM work_status WHERE id = #{workStatusId}")
    @Results(id = "getStatusNameMap", value = {
            @Result(property = "work_status_Name", column = "name"),})
    String getStatusName(@Param("work_status_id")int workStatusId);

    @Select("SELECT name FROM user_info WHERE user_id = #{appointorId}")
    @Results(id = "getAppointorNameMap", value = {
            @Result(property = "appointorName", column = "name"),})
    String getAppointorName(@Param("appointor_id")String appointorId);

    @Insert("insert into attachments(`id`,`name`,`hash`,`type`,`size`,`path`) values (#{id},#{name},#{hash},#{type},#{size},#{path})")
    int insertUserAttachment(Attachments attachments);
}
